﻿import re
import chardet
import sys

# 定义 Twips 到像素的转换函数
def twips_to_pixels(twips, ppi=96):
    return int(twips / 1440 * ppi)

# 初始化 HMENU 计数器
hmenu_counter = 1

# 检查命令行参数
if len(sys.argv) < 2:
    print("请提供一个 .frm 文件路径作为参数。")
    sys.exit(1)

file_path = sys.argv[1]

# 读取文件内容并自动检测编码
try:
    with open(file_path, "rb") as file:
        raw_data = file.read()
        encoding = chardet.detect(raw_data)['encoding']
except FileNotFoundError:
    print(f"文件未找到: {file_path}")
    sys.exit(1)

# 使用检测到的编码重新打开文件
with open(file_path, "r", encoding=encoding) as file:
    content = file.read()

# 定义正则表达式模式
form_pattern = r"""
    Caption\s*=\s*"([^"]*)"\s*       # 匹配 Caption 和其值
    ClientHeight\s*=\s*(\d+)\s*      # 匹配 ClientHeight 和其值
    ClientLeft\s*=\s*(\d+)\s*        # 匹配 ClientLeft 和其值
    ClientTop\s*=\s*(\d+)\s*         # 匹配 ClientTop 和其值
    ClientWidth\s*=\s*(\d+)\s*       # 匹配 ClientWidth 和其值
    LinkTopic\s*=\s*"([^"]*)"\s*     # 匹配 LinkTopic 和其值
    ScaleHeight\s*=\s*(\d+)\s*       # 匹配 ScaleHeight 和其值
    ScaleWidth\s*=\s*(\d+)\s*        # 匹配 ScaleWidth 和其值
    StartUpPosition\s*=\s*(\d+)\s*   # 匹配 StartUpPosition 和其值
"""

control_pattern = r"""
    Begin\s+VB\.(TextBox|CommandButton|Label|CheckBox|Timer|ProgressBar)\s+(\w+)\s*     # 匹配控件类型和名称
    (.*?)                                                            # 匹配控件的属性部分
    End\s*                                                           # 匹配 End
"""

control_ProgressBar = r"""
    Begin\s+MSComctlLib\.(ProgressBar)\s+(\w+)\s*     # 匹配控件类型和名称
    (.*?)                                                            # 匹配控件的属性部分
    End\s*                                                           # 匹配 End
"""

# 提取 Form 属性
form_matches = re.findall(form_pattern, content, re.DOTALL | re.VERBOSE)
if form_matches:
    for match in form_matches:
        caption = match[0].strip()
        client_width = twips_to_pixels(int(match[4].strip()))
        client_height = twips_to_pixels(int(match[1].strip()))
        print(f'HWND hwndWindows = CreateWindows(hInstance, windowClass, windowTitle, {client_width}, {client_height});')
    print()

# 提取控件属性
control_matches = re.findall(control_pattern, content, re.DOTALL | re.VERBOSE)
for control_type, control_name, control_content in control_matches:
    control_content = control_content.strip()
    properties = {}
    for line in control_content.splitlines():
        key, value = re.split(r'\s*=\s*', line.strip(), maxsplit=1)
        properties[key] = value.strip()

    left = twips_to_pixels(int(properties.get('Left', 0)))
    top = twips_to_pixels(int(properties.get('Top', 0)))
    width = twips_to_pixels(int(properties.get('Width', 0)))
    height = twips_to_pixels(int(properties.get('Height', 0)))
    caption = properties.get('Caption', '').strip()

    # 为每个控件分配唯一的 HMENU 值
    hmenu_value = hmenu_counter
    hmenu_counter += 1

    if control_type == 'TextBox':
        print(f'HWND hwnd{control_name} = CreateTextBox(hwndWindows, {left}, {top}, {width}, {height}, hInstance, FALSE, HMENU({hmenu_value}));')
    elif control_type == 'CommandButton':
        print(f'HWND hwnd{control_name} = CreateButton(hwndWindows, L{caption}, {left}, {top}, {width}, {height}, hInstance, HMENU({hmenu_value}));')
    elif control_type == 'Label':
        print(f'HWND hwnd{control_name} = CreateLabel(hwndWindows, L{caption}, {left}, {top}, {width}, {height}, hInstance);')
    elif control_type == 'CheckBox':
        print(f'HWND hwnd{control_name} = CreateCheckBox(hwndWindows, L{caption}, {left}, {top}, {width}, {height}, hInstance, HMENU({hmenu_value}));')
    elif control_type == 'Timer':
        print(f'UINT_PTR hwnd{control_name} = CreateTimer(hwndWindows, L{caption}, HMENU({hmenu_value}), 1000);')
        
# 提取控件属性
control_matches = re.findall(control_ProgressBar, content, re.DOTALL | re.VERBOSE)
for control_type, control_name, control_content in control_matches:
    control_content = control_content.strip()
    properties = {}
    for line in control_content.splitlines():
        key, value = re.split(r'\s*=\s*', line.strip(), maxsplit=1)
        properties[key] = value.strip()

    left = twips_to_pixels(int(properties.get('Left', 0)))
    top = twips_to_pixels(int(properties.get('Top', 0)))
    width = twips_to_pixels(int(properties.get('Width', 0)))
    height = twips_to_pixels(int(properties.get('Height', 0)))
    caption = properties.get('Caption', '').strip()
    
    # 为每个控件分配唯一的 HMENU 值
    hmenu_value = hmenu_counter
    hmenu_counter += 1
    if control_type == 'ProgressBar':
        print(f'HWND hwnd{control_name} = CreateProgressBar(hwndWindows, L{caption}, {left}, {top}, {width}, {height}, hInstance, HMENU({hmenu_value}));')

